import {createStore} from 'redux'
let init={
    list:JSON.parse(localStorage.getItem('list')).list||[],
    sum:0
}
function reducer(state=init,action){
    if(action.type=='JIA'){
        let index=state.list.findIndex((item)=>item.id==action.id)
        state.list[index].num++
        return {
            ...state,
            list:[...state.list]
        }
    }
    if(action.type=='JIAN'){
        let index=state.list.findIndex((item)=>item.id==action.id)
        state.list[index].num--
        if(state.list[index].num<=0){
            state.list.splice(index,1)
        }
        return {
            ...state,
            list:[...state.list]
        }
    }
    return state
}
const store=createStore(reducer)
export default store